package top.healthylife.gateway.platform.br.api.api.basicdata;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.basics.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.healthylife.gateway.core.service.ParentFace;
import top.healthylife.gateway.dto.annotation.ApiServiceExplain;
import top.healthylife.gateway.platform.br.api.utils.GetmeltingTokenUtils;
import top.healthylife.gateway.platform.br.dto.basicdata.courtssubject.req.BrReqCourtsSubjectDto;
import top.healthylife.gateway.platform.br.dto.basicdata.courtssubject.res.BrResCourtsSubjectDto;
import top.healthylife.gateway.platform.br.dto.basicdata.courtssubject.res.CourtBad;
import top.healthylife.gateway.platform.br.dto.basicdata.courtssubject.res.CourtExecuted;
import top.healthylife.gateway.platform.br.dto.basicdata.courtssubject.res.Execution;

import java.util.ArrayList;
import java.util.List;

/**
 * @author cl
 * @version 1.0
 * @date 2020/4/22 16:52
 */
@Slf4j
@Service
@ApiServiceExplain( docUrl="https://account-self.100credit.com/", price = 20, apiName = "法院被执行人-个人版", info = "查询个人的法院失信被执行人、被执行人的执行案件信息。")
public class CourtsSubjectApi implements ParentFace<BrReqCourtsSubjectDto, BrResCourtsSubjectDto> {
    /**
     * 接口唯一标识
     */
    static final String APIIDENTIFICATION = "Execution";

    /**
     * 接口版本号
     */
    static final String VESION = "1.0";

    @Override
    public BrResCourtsSubjectDto execute(BrReqCourtsSubjectDto brDemoApi) {
        //访问百融获取数据
        String apiData = GetmeltingTokenUtils.getApiData(APIIDENTIFICATION, VESION
                , BeanUtil.beanToMap(brDemoApi), GetmeltingTokenUtils.systemConfigInfo(brDemoApi));
        JSONObject jsonObject = JSONObject.parseObject(apiData);
        String code = jsonObject.getString("code");
        if (code.equals("00")) {
            JSONObject jsonExecution = jsonObject.getJSONObject("Execution");
            String propertyOnePrefix = "court_bad";
            String propertyTwoPrefix = "court_executed";
            List<ExecutionInfo> executionInfos = new ArrayList<>();
            BrResCourtsSubjectDto dto;
            try {
                for (int i = 1; i <= 10; i++) {
                    ExecutionInfo executionInfo = new ExecutionInfo();
                    CourtBad courtBad = jsonExecution.getObject(propertyOnePrefix + i, CourtBad.class);
                    CourtExecuted courtExecuted = jsonExecution.getObject(propertyTwoPrefix + i, CourtExecuted.class);
                    if (courtBad == null && courtExecuted == null) {
                        continue;
                    }
                    executionInfo.setCourtBad(courtBad).setCourtExecuted(courtExecuted);
                    executionInfos.add(executionInfo);
                }
                dto = jsonObject.toJavaObject(BrResCourtsSubjectDto.class);
                Execution execution = new Execution().setExecutionList(executionInfos);
                dto.setExecution(execution);
            } catch (Exception e) {
                throw new BusinessException("返回的结构异常");
            }
            return dto;
        } else {
            String swift_number = jsonObject.getString("swift_number");
            String flag_execution = jsonObject.getString("flag_execution");
            BrResCourtsSubjectDto brResCourtsSubjectDto = new BrResCourtsSubjectDto();
            brResCourtsSubjectDto.setCode(Integer.parseInt(code))
                    .setSwiftNumber(swift_number);
            brResCourtsSubjectDto.setFlag(new Flag().setExecution(flag_execution));
            return brResCourtsSubjectDto;
        }
    }
}
